#include "dao.h"

#include <cassert>
#include <QJsonObject>
#include <QJsonDocument>
#include <QPaintEvent>
#include <QFile>

dao::dao() {
    data_file.open(QIODevice::ReadOnly | QIODevice::Text);
    assert(data_file.isOpen());
    load();
}


void dao::save(int score) {
    data_file.open(QIODevice::WriteOnly | QIODevice::Text);
    QJsonObject obj;
    QDateTime t = QDateTime::currentDateTime();
    obj["time"] = t.toString();
    obj["score"] = score;
    history_socre_.append(obj);

    QJsonDocument doc(history_socre_);
    QByteArray array = doc.toJson();
    data_file.write(array);
}
void dao::load() {

    QByteArray read_all = data_file.readAll();

    QJsonDocument doc = QJsonDocument::fromJson(read_all);
    history_socre_ = doc.array();
    data_file.close();
}
